package com.sskj.finance.ui.activity;

import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.SimpleOnPullListener;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.finance.R;
import com.sskj.finance.R2;
import com.sskj.finance.bean.FinanInfoBean;
import com.sskj.finance.presenter.FinanceInfoActivityPresenter;
import com.sskj.finance.widget.DrawRing;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.NumberUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;

@Route(path = RConfig.FINAN_INFO)
public class FinanceInfoActivity extends BaseActivity<FinanceInfoActivityPresenter> {
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    @Autowired
    String id;
    @Autowired
    String name;
    @BindView(R2.id.tvRate)
    TextView tvRate;
    @BindView(R2.id.tvDay)
    TextView tvDay;
    @BindView(R2.id.tvMin)
    TextView tvMin;
    @BindView(R2.id.tvMax)
    TextView tvMax;
    @BindView(R2.id.tvRealProfile)
    TextView tvRealProfile;
    @BindView(R2.id.tvStart)
    TextView tvStart;
    @BindView(R2.id.tvCenter)
    TextView tvCenter;
    @BindView(R2.id.tvEnd)
    TextView tvEnd;
    @BindView(R2.id.tvUnGet)
    TextView tvUnGet;
    @BindView(R2.id.tvGet)
    TextView tvGet;
    @BindView(R2.id.tvRateCenter)
    TextView tvRateCenter;
    @BindView(R2.id.tvType)
    TextView tvType;
    @BindView(R2.id.drawRing)
    DrawRing drawRing;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_finance_info;
    }

    @Override
    public FinanceInfoActivityPresenter getPresenter() {
        return new FinanceInfoActivityPresenter();
    }

    @Override
    protected void onResume() {
        if (mPresenter != null) {
            mPresenter.finanInfo(id);
        }
        super.onResume();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        setTitle(name);
        coolRefreshView.addOnPullListener(new SimpleOnPullListener() {
            @Override
            public void onRefreshing(CoolRefreshView refreshView) {
                mPresenter.finanInfo(id);
            }
        });
    }

    @Override
    protected void initData() {
        mPresenter.finanInfo(id);
    }

    public void updateUI(FinanInfoBean data) {
        if (coolRefreshView != null && coolRefreshView.isRefreshing()) {
            coolRefreshView.setRefreshing(false);
        }
        tvRate.setText(data.getExpect_rate() + "%");
        tvDay.setText(data.getDays() + App.INSTANCE.getString(R.string.finan_myOrderActivity6_j));
        tvType.setText(data.getType().equals("1") ? App.INSTANCE.getString(R.string.finan_financeInfoActivity2_j) : App.INSTANCE.getString(R.string.finan_financeInfoActivity3_j));
        tvMin.setText(String.format(App.INSTANCE.getString(R.string.finan_financeInfoActivity4_j), data.getNum_min()));
        tvMax.setText(String.format(App.INSTANCE.getString(R.string.finan_financeInfoActivity5_j), data.getNum_total()));
        tvRealProfile.setText(data.getReal_rate() + "%");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        Date endDate = null;
        try {
            endDate = simpleDateFormat.parse(data.getEndtime().substring(data.getEndtime().indexOf("-") + 1));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar instance = Calendar.getInstance();
        instance.setTime(endDate);
        instance.add(Calendar.DAY_OF_YEAR, Integer.valueOf(data.getDays()));
        String format = simpleDateFormat.format(instance.getTime());
        tvStart.setText(data.getStarttime().substring(data.getStarttime().indexOf("-") + 1));
        if (data.getStatus().equals("1")) {

            tvCenter.setText(data.getEndtime().substring(data.getEndtime().indexOf("-") + 1));
            tvEnd.setText(format);
        }else {

            tvCenter.setText(data.getStart().substring(data.getStart().indexOf("-") + 1));
            tvEnd.setText(data.getEnd().substring(data.getEnd().indexOf("-") + 1));
        }
        ClickUtil.click(btSubmit, () -> {
            if (data.getStatus().equals("1")){

                ARouter.getInstance().build(RConfig.FINAN_APPOINTMENT_BUY)
                    .withString(Constans.ID, id)
                    .withString(Constans.NAME, data.getMemo())
                    .withString(Constans.DATE, tvCenter.getText().toString().replace("-", App.INSTANCE.getString(R.string.finan_financeInfoActivity6_j)) + App.INSTANCE.getString(R.string.finan_financeInfoActivity7_j))
                    .withString(Constans.MIN, data.getNum_min())
                    .withString(Constans.RATE, data.getExpect_rate())
                    .navigation();
            }else {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.finan_financeInfoActivity8_j));
            }
        });
        tvRateCenter.setText(data.getRate() + "%");
        drawRing.setRate(data.getRate());
        tvGet.setText(NumberUtil.keepNoZero(data.getNum_hold()));
        double v = Double.valueOf(data.getNum_total()) - Double.valueOf(data.getNum_hold());
        double unget = Math.max(0, v);
        tvUnGet.setText(NumberUtil.keepNoZero(Double.valueOf(unget).intValue() + ""));

    }

}
